"""
Case Type   : alter procedure
Case Name   : 元命令\h查询alter procedure语法帮助
Create At   : 2024.5.25
Owner       : @zou_jialiang050
Description :
    1、创建兼容b库
    2、b库下执行\h alter procedure
Expect      :
    1、成功
    2、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CompatibilityTest001(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.conf_path = os.path.join(macro.DB_INSTANCE_PATH,
                                      macro.DB_PG_CONFIG_NAME)
        self.constant = Constant()
        self.db_name = 'db_alter_procedure54'

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:创建兼容b库;expect:创建成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      "执行失败" + text)

        text = '----step2:b库下执行\h alter procedure;expect:成功----'
        self.log.info(text)
        sql_cmd = "\h alter procedure"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.user_node.db_name)
        self.log.info(sql_res)
        msg = ''
        self.assertIn(msg, sql_res, '执行失败')

    def tearDown(self):
        self.log.info('----step1:清理环境;expect:成功----')
        sql_cmd = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res, "执行失败")
        self.log.info(f'----{os.path.basename(__file__)} end-----')
